package me.firstapp.manage.order;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.json.AbstractJsonObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonDistributeCall;
import me.firstapp.common.module.shop.JsonOrder;
import me.firstapp.common.module.shop.JsonOrderInManage;
import me.firstapp.common.module.shop.JsonOrderItemInManage;
import me.firstapp.common.module.shop.JsonPayRecord;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.manage.common.BaseManage;
import me.firstapp.manage.utils.BizData4Page;
import me.firstapp.spi.shop.OrderService;

@Controller
@RequestMapping(value = "/orderManage")
public class OrderManageAct extends BaseManage {

	@Autowired
	private OrderService orderService;

	@ResponseBody
	@RequestMapping(value = "/findOrderPage.do", method = RequestMethod.POST)
	public void findOrderPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			String startTime = null;
			String endTime = null;
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						if ("createTime".equals(key)) {
							// 日期范围判断
							String registerTimes = (String) jsonObject.get(key);
							if (registerTimes.contains(",")) {
								String[] registerTimesArra = registerTimes.split(",");
								startTime = registerTimesArra[0];
								endTime = registerTimesArra[1];
								if ("0".equals(startTime)) {
									startTime = null;
								}
								if ("0".equals(endTime)) {
									endTime = null;
								}
							}
						} else {
							searchParams.put(key, jsonObject.get(key));
						}
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				PageObject<JsonOrderInManage> orderPage = orderService.findOrderPage(pageNo, iDisplayLength,
						searchParams, mDataProp, sSortDir_0,
						StrUtils.isNULL(startTime) ? null : Long.valueOf(startTime),
						StrUtils.isNULL(endTime) ? null : Long.valueOf(endTime));
				SingleObject<Object> result = new SingleObject<Object>();
				BizData4Page<JsonOrderInManage> bizData4Page = new BizData4Page<JsonOrderInManage>();
				bizData4Page.setUserdata(conditions);
				if (orderPage.getPage() != null) {
					bizData4Page.setPage(orderPage.getPage().getPageNo());
					bizData4Page.setPagesize(orderPage.getPage().getPageSize());
					bizData4Page.setRecords(orderPage.getPage().getTotalCount());
					bizData4Page.setRows(new ArrayList<JsonOrderInManage>(orderPage.getPage().getList()));
					bizData4Page.setTotal(orderPage.getPage().getTotalCount());
				}

				result.setCode(orderPage.getCode());
				result.setMsg(orderPage.getMsg());
				result.setObject(bizData4Page);
				ResponseUtils.renderJson(response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
			abstractJsonObject.setCode("error");
			abstractJsonObject.setMsg("发生未知错误");
			ResponseUtils.renderJson(response, abstractJsonObject);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/findOrderItemPage.do", method = RequestMethod.POST)
	public void findOrderItemPage(@RequestParam Map<String, Object> conditions, Long orderId,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						searchParams.put(key, jsonObject.get(key));
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				PageObject<JsonOrderItemInManage> orderItemPage = orderService.findOrderItemPage(pageNo, iDisplayLength,
						searchParams, mDataProp, sSortDir_0, orderId);
				SingleObject<Object> result = new SingleObject<Object>();
				BizData4Page<JsonOrderItemInManage> bizData4Page = new BizData4Page<JsonOrderItemInManage>();
				bizData4Page.setUserdata(conditions);
				if (orderItemPage.getPage() != null) {
					bizData4Page.setPage(orderItemPage.getPage().getPageNo());
					bizData4Page.setPagesize(orderItemPage.getPage().getPageSize());
					bizData4Page.setRecords(orderItemPage.getPage().getTotalCount());
					bizData4Page.setRows(new ArrayList<JsonOrderItemInManage>(orderItemPage.getPage().getList()));
					bizData4Page.setTotal(orderItemPage.getPage().getTotalCount());
				}

				result.setCode(orderItemPage.getCode());
				result.setMsg(orderItemPage.getMsg());
				result.setObject(bizData4Page);
				ResponseUtils.renderJson(response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
			abstractJsonObject.setCode("error");
			abstractJsonObject.setMsg("发生未知错误");
			ResponseUtils.renderJson(response, abstractJsonObject);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/findPayRecordPage.do", method = RequestMethod.POST)
	public void findPayRecordPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			String startTime = null;
			String endTime = null;
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						if ("payTime".equals(key)) {
							// 日期范围判断
							String registerTimes = (String) jsonObject.get(key);
							if (registerTimes.contains(",")) {
								String[] registerTimesArra = registerTimes.split(",");
								startTime = registerTimesArra[0];
								endTime = registerTimesArra[1];
								if ("0".equals(startTime)) {
									startTime = null;
								}
								if ("0".equals(endTime)) {
									endTime = null;
								}
							}
						} else {
							searchParams.put(key, jsonObject.get(key));
						}
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				PageObject<JsonPayRecord> payRecordPage = orderService.findPayRecordPage(pageNo, iDisplayLength,
						searchParams, mDataProp, sSortDir_0,
						StrUtils.isNULL(startTime) ? null : Long.valueOf(startTime),
						StrUtils.isNULL(endTime) ? null : Long.valueOf(endTime));
				SingleObject<Object> result = new SingleObject<Object>();
				BizData4Page<JsonPayRecord> bizData4Page = new BizData4Page<JsonPayRecord>();
				bizData4Page.setUserdata(conditions);
				if (payRecordPage.getPage() != null) {
					bizData4Page.setPage(payRecordPage.getPage().getPageNo());
					bizData4Page.setPagesize(payRecordPage.getPage().getPageSize());
					bizData4Page.setRecords(payRecordPage.getPage().getTotalCount());
					bizData4Page.setRows(new ArrayList<JsonPayRecord>(payRecordPage.getPage().getList()));
					bizData4Page.setTotal(payRecordPage.getPage().getTotalCount());
				}

				result.setCode(payRecordPage.getCode());
				result.setMsg(payRecordPage.getMsg());
				result.setObject(bizData4Page);
				ResponseUtils.renderJson(response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
			abstractJsonObject.setCode("error");
			abstractJsonObject.setMsg("发生未知错误");
			ResponseUtils.renderJson(response, abstractJsonObject);
		}
	}

	@RequestMapping(value = "/cancelOrder.do", method = RequestMethod.POST)
	public void cancelOrder(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<JsonOrder> result = orderService.cancelOrder(orderNo);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/completeOrder.do", method = RequestMethod.POST)
	public void completeOrder(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<JsonOrder> result = orderService.completeOrder(orderNo);
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/distributeOrder.do", method = RequestMethod.POST)
	public void distributeOrder(String orderNo, String expressName, String expressNo, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<JsonOrder> result = orderService.distributeOrder(orderNo, expressName, expressNo);
		ResponseUtils.renderJson(response, result);
	}

	@ResponseBody
	@RequestMapping(value = "/findDistributeCallPage.do", method = RequestMethod.POST)
	public void findDistributeCallPage(@RequestParam Map<String, Object> conditions, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			// 搜索条件
			Map<String, Object> searchParams = new HashMap<>();
			String sSearchJson = (String) conditions.get("sSearch");
			if (StrUtils.isNotNULL(sSearchJson)) {
				JSONObject jsonObject = JSONObject.parseObject(sSearchJson);
				if (jsonObject != null) {
					Set<String> keySet = jsonObject.keySet();
					for (String key : keySet) {
						searchParams.put(key, jsonObject.get(key));
					}
				}
			}
			// 排序字段
			String mDataProp = "mDataProp_" + conditions.get("iSortCol_0");
			mDataProp = conditions.get(mDataProp).toString().replace("Str", "");
			// 排序方式
			String sSortDir_0 = (String) conditions.get("sSortDir_0");
			// 页容
			String iDisplayLengthStr = (String) conditions.get("iDisplayLength");
			// 是开始数不是当前页数...
			String iDisplayStartStr = (String) conditions.get("iDisplayStart");
			if (StrUtils.isNotNULL(iDisplayLengthStr) && StrUtils.isNotNULL(iDisplayStartStr)) {
				Integer iDisplayLength = Integer.valueOf(iDisplayLengthStr);
				Integer iDisplayStart = Integer.valueOf(iDisplayStartStr);
				// 当前页数
				Integer pageNo = iDisplayStart / iDisplayLength + 1;
				PageObject<JsonDistributeCall> callPage = orderService.findDistributeCallPage(pageNo, iDisplayLength,
						searchParams, mDataProp, sSortDir_0);
				SingleObject<Object> result = new SingleObject<Object>();
				BizData4Page<JsonDistributeCall> bizData4Page = new BizData4Page<JsonDistributeCall>();
				bizData4Page.setUserdata(conditions);
				if (callPage.getPage() != null) {
					bizData4Page.setPage(callPage.getPage().getPageNo());
					bizData4Page.setPagesize(callPage.getPage().getPageSize());
					bizData4Page.setRecords(callPage.getPage().getTotalCount());
					bizData4Page.setRows(new ArrayList<JsonDistributeCall>(callPage.getPage().getList()));
					bizData4Page.setTotal(callPage.getPage().getTotalCount());
				}

				result.setCode(callPage.getCode());
				result.setMsg(callPage.getMsg());
				result.setObject(bizData4Page);
				ResponseUtils.renderJson(response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			AbstractJsonObject abstractJsonObject = new AbstractJsonObject();
			abstractJsonObject.setCode("error");
			abstractJsonObject.setMsg("发生未知错误");
			ResponseUtils.renderJson(response, abstractJsonObject);
		}
	}

	@RequestMapping(value = "/handleDistributeCall.do", method = RequestMethod.POST)
	public void handleDistributeCall(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = orderService.handleDistributeCall(orderNo);
		ResponseUtils.renderJson(response, result);
	}

}
